package kr.hkit.powerjava.chapter22.submit2.dao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; //ArrayList의 부모가 List. 써도됨

import kr.hkit.powerjava.chapter22.submit2.vo.Student;

public class StudentManagerImpl implements ItemDaoInterface<Student> {
	private List<Student> arList;
	
	public StudentManagerImpl(ArrayList<Student> arList) {
		this.arList = arList;
	}

	@Override
	public void addItem(Student item) {
		arList.add(item);
	}

	@Override
	public void delItem(Student item) {
		arList.remove(item);
	}

	@Override
	public void updateItem(Student item) {
		if (searchItem(item)) { // 존재한다면 수정 
		arList.set(arList.indexOf(item), item);
		}else{
			System.out.println("수정 실패");
		}
	}

	@Override
	public boolean searchItem(Student item) { //불리언 쓰는게 편함 
		return (arList.contains(item)); /*{ //참이면 존재한다
			std=arList.get(arList.indexOf(item)); 
		}
		return std;//
*/	}

	@Override
	public void prnItems() {
		prnHeader();
//		Collections.sort(arList); //총점순 정렬이 기본값 
		for (Student s : arList) {
			System.out.println(s);
		}
		System.out.println();
	}


	private void prnHeader() {
		 String str = String
			.format("%3s\t%5s\t%3s\t%3s\t%3s\t%5s\t%5s",
					"학번", "성명", "국어", "영어", "수학", "총점", "평균");
		 System.out.println(str);
		 System.out.println("--------------------------------------------------------");
	}

	
	
	@Override //스튜던트클래스에서 comparable 했음
	public void prnItems(Comparator<Student> comp) {  
		Collections.sort(arList, comp); 
		prnItems();
	}

}